<?php
namespace admin\action;

use herosphp\bean\Beans;
use herosphp\http\HttpRequest;
use herosphp\string\StringUtils;
use herosphp\utils\AjaxResult;

/**
 * user action
 * @package admin\action
 * @author liuyi<liuyi@pvc123.com>
 */
class UserAction extends CommonAction {

    protected $serviceBean = 'user.user.service';
    protected $actionTitle = '会员';

    //数据列表页面
    public function index( HttpRequest $request ) {
        $username = $request->getParameter('username', 'trim');
        $from = $request->getParameter('from', 'trim');
        $starttime = $request->getParameter('starttime', 'trim');
        $endtime = $request->getParameter('endtime', 'trim');

        if ( $from === '' ) {
            $from = '-1';
            $request->addParameter('status', '-1');
        }

        $conditions = array();
        if ( $username ) $conditions['username'] = "%{$username}%";
        if ( $from != '-1' ) $conditions['from'] = $from;
        if ( $starttime ) $conditions['addtime']['>'] = $starttime.' 00:00:00';
        if ( $endtime ) $conditions['addtime']['<'] = $endtime.' 24:00:00';

        $this->setConditions($conditions);

        if( $request->getParameter('__export') == 1 ) {
            $this->assign('tableHeader', array('会员号','手机号码','来源','会员状态','注册时间'));
            $this->export($request); //导出数据
        }
        parent::index($request);
        $this->setView('user/user_index');
        $this->assign('cstatus', array('1' => '官网注册', '2' => '报价应用','3'=>"后台添加"));
        $this->assign("title", $this->actionTitle."列表");
        $this->assign('breadcrumb', array('会员管理',$this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;','javascript:;'));
    }

    //数据添加页面
    public function add(HttpRequest $request) {
        throw new UnSupportedOperationException();
    }

    //数据编辑页面
    public function edit(HttpRequest $request) {

        $id = $request->getParameter('id', 'trim');
        if ( empty($id) ) {
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, self::NO_RECORDS);
        }
        $service = Beans::get($this->getServiceBean());
        $item = $service->getItem($id);
        if ( empty($item) ) {
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, self::NO_RECORDS);
        }
        AjaxResult::ajaxResult(AjaxResult::OP_SUCCESS, null, $item);
    }

    //插入数据操作
    public function insert(HttpRequest $request) {

        $data = $request->getParameter('data');
        $service = Beans::get($this->getServiceBean());
        if( $service->getItem(array('username'=>$data['username'])) ){
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,"该手机号码已经注册!");
        }
        $data['id'] = StringUtils::genGlobalUid(); //生成密钥
        $data['name'] = 'saas_'.substr($data['username'],4);
        $data['from'] = 3;//后台添加的
        $data['password'] = passwd($data['password']);
        parent::__insert($data);
    }

    //更新数据操作
    public function update(HttpRequest $request) {

        $data = $request->getParameter('data');
        if( !$data['password'] ){
            unset($data['password']);
        }else{
            $data['password'] = passwd($data['password']);
        }
        $id = $request->getParameter('id', 'trim');
        if ( empty($id) ) {
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, self::NO_RECORDS);
        }
        parent::__update($data, $id);

    }

    public function delete(HttpRequest $request)
    {
        parent::delete($request); // TODO: Change the autogenerated stub
    }

    //导出数据
    protected function export(HttpRequest $request) {
        if( $request->getParameter('__export') == 1 ){
            $ids = $request->getParameter('ids');
            if( $ids ){
                $conditions['id']['$in']=$ids;
                $this -> setConditions($conditions);
            }
            $this->setPagesize(getConfig('export_max_count'));
            $service = Beans::get($this->getServiceBean());
            $items = $service->where($this->getConditions())->limit(1, $this->getPagesize())->sort(array('addtime' => -1))->find();
            $excel = array();
            $cstatus= array('1' => '官网注册', '2' => '报价应用','3'=>"后台添加");
            foreach($items as $k => $v)
            {
                $tmp = array();
                $tmp[] = $v["id"];
                $tmp[] = $v["username"];
                $tmp[] = $cstatus[$v["from"]];
                $tmp[] = $v['status'] == 1 ? '启用' : '禁用';
                $tmp[] = $v["addtime"];
                $excel[] = $tmp;
            }
            $header = $this->getTemplateVar('tableHeader');
            phpexcel("会员列表",$header,$excel);
            exit();
        }
    }
}
